package com.example.icongyou.widget;

// GradientTextView.java
import android.content.Context;
import android.graphics.LinearGradient;
import android.graphics.Shader;
import android.util.AttributeSet;
import androidx.appcompat.widget.AppCompatTextView;

public class GradientTextView extends AppCompatTextView {

    public GradientTextView(Context context) {
        super(context);
        init();
    }

    public GradientTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public GradientTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        // 确保在布局完成后设置渐变
        post(this::setGradient);
    }

    private void setGradient() {

        int startColor = 0xFFFF1493; // 深粉色
        int endColor = 0xD26CCEFF;

        Shader shader = new LinearGradient(
                0, 0, getWidth(), 0,
                startColor, endColor,
                Shader.TileMode.CLAMP
        );

        getPaint().setShader(shader);
        invalidate(); // 重绘
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if (changed) {
            setGradient();
        }
    }
}